﻿using Microsoft.Extensions.Options;
using RuoVea.ExEnum;
using RuoVea.ExFilter;
using RuoVea.ExSugar;
using RuoVea.OmiApi.Log.Entitys;
using exDomain = RuoVea.ExFilter.Domain;

namespace RuoVea.OmiApi.Log;

/// <summary>
/// 统一拦截日志记录
/// </summary>
public class ICustomizeFilterLog : RestfulFilterLog
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public readonly SqlSugar.SqlSugarScope dbContext;
    private readonly List<DbConnectionConfig> _dbConnection;

    /// <summary>
    /// 
    /// </summary>
    public ICustomizeFilterLog(IOptions<List<DbConnectionConfig>> dbConnection)
    {
        _dbConnection = ExSugar.Util.SugarUtil.GetDbConfig(dbConnection.Value);
        dbContext = ExSugar.Util.SugarUtil.GetSqlSugarScope(_dbConnection);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="operation"></param>
    public override void OperationLog(exDomain.OperationVo operation)
    {
        base.OperationLog(operation);

        if (LogOp(operation))
        {
            dbContext.Insertable(new SysLogOp
            {
                ClassName = operation.ControllerName + "/" + operation.ActionName,
                Name = operation.ActionDescriptorName,
                MethodName = operation.OpType.ToString(),
                Success = operation.Success ?? YesOrNot.N,
                Message = operation.Message,
                Ip = operation.IpAddress,
                Location = operation.RequestUrl,
                Browser = operation.Browser,
                Os = operation.Os,
                Url = operation.Url,
                ReqMethod = operation.RequestType,
                Param = operation.Param,
                Result = operation.Result,
                ElapsedTime = operation.ElapsedTime ?? 0,
                OpTime = operation.OperationTime ?? DateTime.Now,
                Account = operation.TenantId + ":" + operation.userId + "-" + operation.Account,
            }).ExecuteCommand();
        }
        if (LogVis(operation))
        {
            dbContext.Insertable(new SysLogVis
            {
                Name = operation.ActionDescriptorName,
                Success = operation.Success ?? YesOrNot.N,
                Message = operation.Message,
                Ip = operation.IpAddress,
                Location = operation.RequestUrl,
                Browser = operation.Browser,
                Os = operation.Os,
                Account = operation.TenantId + ":" + operation.userId + "-" + operation.Account,
                VisType = LoginType.LOGIN,
                VisTime = DateTime.Now
            }).ExecuteCommand();
        }
    }

    /// <summary>
    /// 访问日志
    /// </summary>
    /// <param name="operation"></param>
    /// <returns></returns>
    public virtual bool LogVis(exDomain.OperationVo operation)
    {
        return false;
    }

    /// <summary>
    /// 操作日志
    /// </summary>
    /// <param name="operation"></param>
    /// <returns></returns>
    public virtual bool LogOp(exDomain.OperationVo operation)
    {
        return false;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="exception"></param>
    public override void ExceptionLog(exDomain.ExceptionVo exception)
    {
        base.ExceptionLog(exception);
        dbContext.Insertable(new SysLogEx
        {
            Account = exception.Account,
            Name = exception.Name,
            ClassName = exception.ClassName,
            MethodName = exception.MethodName,
            ExceptionMsg = exception.ExceptionMessage,
            ExceptionSource = exception.ExceptionSource,
            StackTrace = exception.StackTrace,
            ParamsObj = exception.ParamsObj,
            ExceptionTime = exception.ExceptionTime ?? DateTime.Now,
        }).ExecuteCommand();
    }
}
